package bbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.HeaderViewListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.UIHandler;

import java.util.ArrayList;
import java.util.List;

import bbs.adapter.SubmitsAdapter;
import bbs.fragment.RefreshLayout;
import bbs.model.Submit;
import bbs.net.GetMoreSubmits;
import bbs.net.GetNewSubmit;
import bbs.net.GetSubmitBySubject;

/**
 * 某一主题下的所有帖子
 * Created by nimi on 2015/11/12.
 */
public class OneSubjectActivity extends UIHandler.BaseActivity implements GetSubmitBySubject.Delegate,SwipeRefreshLayout.OnRefreshListener,GetNewSubmit.Delegate ,GetMoreSubmits.Delegate {
    private Button back_bbs;
    private Button newpost;
    private TextView bbsTitle;
    private ListView listsubmits;
    private List<Submit> submits=new ArrayList<Submit>();
    private GetSubmitBySubject getSubmitBySubject;
    private static final int REFRESH_COMPLETE = 0X110;//检测刷新
    private static final int MORE_GET = 0X111;//检测上拉
    private RefreshLayout mSwipeLayout;
    SubmitsAdapter submitsAdapter;
    String subjectName;
    long subjectID;
    String firstTime;//刷新需要的上传数据
    String lastTime;//上拉需要的上传数据
    private Handler mHandler;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.bbs_board_all);
        Bundle bundle=this.getIntent().getExtras();
        subjectID=bundle.getLong("subjectID");
        subjectName=bundle.getString("subjectNAME");
        mHandler= new Handler()
        {
            public void handleMessage(Message msg)

            {
                super.handleMessage(msg);
                switch (msg.what)
                {
                    case REFRESH_COMPLETE:

                        submitsAdapter.clean();
                        getSubmitBySubject=new GetSubmitBySubject(subjectID);
                        getSubmitBySubject.setDelegate(OneSubjectActivity.this);
                        getSubmitBySubject.get();

//                        if(submits.size()>0)
//                            firstTime=submitsAdapter.getItem(0).getBoard_IssueTime();
//                        GetNewSubmit getNewSubmit;
//                        GetNewSubmit.Upload upload=new GetNewSubmit.Upload();
//                        upload.setFirstTime(firstTime);
//                        upload.setSubjectID(subjectID);
//                        getNewSubmit=new GetNewSubmit(upload);
//                        getNewSubmit.setDelegate(OneSubjectActivity.this);
//                        getNewSubmit.get();
                        break;
                    case MORE_GET:
                        int num=submits.size();
                        lastTime=submitsAdapter.getItem(num-1).getBoard_IssueTime();
                        GetMoreSubmits getMoreSubmits;
                        GetMoreSubmits.Upload upload1=new GetMoreSubmits.Upload();
                        upload1.setLastTime(lastTime);
                        upload1.setSubjectID(subjectID);
                        getMoreSubmits=new GetMoreSubmits(upload1);
                        getMoreSubmits.setDelegate(OneSubjectActivity.this);
                        getMoreSubmits.get();
                        break;

                }
            }
        };
        init();
        initSubmits();
    }
    private void init(){
        bbsTitle=(TextView)findViewById(R.id.bbs_title);
        bbsTitle.setText(subjectName);
        back_bbs=(Button)findViewById(R.id.back_bbs);
        back_bbs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(OneSubjectActivity.this, BbsMainActivity.class);
//                startActivity(intent);
                finish();
            }
        });
        mSwipeLayout = (RefreshLayout) findViewById(R.id.id_swipe_ly);//刷新控件
        //mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                System.out.println("刷新,获取更新的数据");
                Message msg = new Message();
                msg.what = 0x110;
                mHandler.sendMessage(msg);
                mSwipeLayout.setRefreshing(false);
            }
        });
//        mSwipeLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
//
//            @Override
//            public void onLoad() {
//                Message msg = new Message();
//                msg.what = 0x111;
//                mHandler.sendMessage(msg);
//                mSwipeLayout.setLoading(false);
//
//            }
//        });
        listsubmits=(ListView)findViewById(R.id.listsubmits);
        submitsAdapter=new SubmitsAdapter(this,submits);
        HeaderViewListAdapter headerViewListAdapter = new HeaderViewListAdapter(null,null,submitsAdapter);
        listsubmits.setAdapter(headerViewListAdapter);
        listsubmits.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Submit submit = submits.get(i);
                Intent intent = new Intent(OneSubjectActivity.this, OnePostActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("submitID", submit.getBoard_ID());
                bundle.putString("subjectID", submit.getTopic_ID());
                bundle.putString("subjectName", subjectName);
                bundle.putString("submitName", submit.getBoard_Title());
                bundle.putString("submitContent", submit.getBoard_Content());
                bundle.putString("submitTime", submit.getBoard_IssueTime());
                bundle.putString("userName", submit.getUser_RealName());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        newpost=(Button)findViewById(R.id.writenewpost);
        newpost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(OneSubjectActivity.this, NewPostActivity.class);
                Bundle bundle = new Bundle();
                bundle.putLong("subjectId", subjectID);
                bundle.putString("subjectName", subjectName);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    public void initSubmits(){
        getSubmitBySubject=new GetSubmitBySubject(subjectID);
        getSubmitBySubject.setDelegate(OneSubjectActivity.this);
        getSubmitBySubject.get();
    }
    @Override
    protected void onStart() {
        super.onStart();
    }
    @Override
    protected void onResume() {
        super.onResume();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    @Override
    protected void onRestart() {
        super.onRestart();
       // initSubmits();
    }
    public void onResponse(String response,GetSubmitBySubject getSubmitBySubject){

            if(getSubmitBySubject.getDownload().getStatus()==1) {
                Toast.makeText(this,"获取数据成功",Toast.LENGTH_SHORT).show();
                submits.addAll(getSubmitBySubject.getDownload().getBbs_boards());
                submitsAdapter.notifyDataSetChanged();
            }

    }
    public void onErrorResponse(VolleyError error, GetSubmitBySubject getSubmitBySubject){

    }
    public void onResponse(String response,GetNewSubmit getNewSubmit) {
        if(getNewSubmit.getDownload().getStatus()==1) {
            if(getNewSubmit.getDownload().getBbs_boards()!=null&&getNewSubmit.getDownload().getBbs_boards().size()>0) {
                submits.addAll(0,getNewSubmit.getDownload().getBbs_boards());
                submitsAdapter.notifyDataSetChanged();
                Toast.makeText(this, "刷新成功", Toast.LENGTH_SHORT).show();
            }
            else if(getNewSubmit.getDownload().getBbs_boards().size()==0){
                Toast.makeText(this,"没有新数据",Toast.LENGTH_SHORT).show();

            }else
                Toast.makeText(this, "网络故障", Toast.LENGTH_SHORT).show();
        }


    }
    public void onErrorResponse(VolleyError error, GetNewSubmit getNewSubmit){

    }
    public void onResponse(String response,GetMoreSubmits getMoreSubmits) {
        if(getMoreSubmits.getDownload().getStatus()==1) {
            if(getMoreSubmits.getDownload().getBbs_boards()!=null&&getMoreSubmits.getDownload().getBbs_boards().size()>0) {
                int num=submits.size();
                submits.addAll(num, getMoreSubmits.getDownload().getBbs_boards());
                submitsAdapter.notifyDataSetChanged();
            }
            else if(getMoreSubmits.getDownload().getBbs_boards().size()==0){
                Toast.makeText(this,"没有更多数据",Toast.LENGTH_SHORT).show();

            }else
                Toast.makeText(this, "网络故障", Toast.LENGTH_SHORT).show();
        }

    }
    public void onErrorResponse(VolleyError error, GetMoreSubmits getMoreSubmits){

    }

        public void onRefresh()
    {
       // mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000);
    }

}
